<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供货类别" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入供货类别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品规格" prop="spec">
        <el-input
          v-model="queryParams.spec"
          placeholder="请输入产品规格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="未处理" value="10" />
          <el-option label="已处理" value="20" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:supplier:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:supplier:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:supplier:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:supplier:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="供货类别" align="center" prop="category" />
      <el-table-column label="产品规格" align="center" prop="spec" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="上传视频" align="center" prop="video">
        <template slot-scope="scope">
          <video :src="scope.row.video" style="width:60px;height:60px;" @click="handleVideo(scope)"></video>
        </template>
      </el-table-column>
      <el-table-column label="商品主图" align="center" prop="images">
        <template slot-scope="scope" v-if="scope.row.images != null">
          <el-image
            :preview-src-list="scope.row.images.split(',')"
            style="width: 30px; height: 30px"
            shape="square"
            :src="image"
            :key="image"
            v-for="image in scope.row.images.split(',')"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="状态 " align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.status == 10">未处理</el-tag>
          <el-tag type="success" v-if="scope.row.status == 20">已处理</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:supplier:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDeal(scope.row)"
          >处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog
      :visible.sync="videoOpen"
      width="500px;"
      append-to-body
      @close="handleVideoClose"
      center>
        <video ref="popVideo" :src="videoOpenUrl" controls style="width:100%;height:100%;"></video>
    </el-dialog>

    <!-- 添加或修改供应商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" disabled placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" disabled placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="供货类别" prop="category">
          <el-input v-model="form.category" disabled placeholder="请输入供货类别" />
        </el-form-item>
        <el-form-item label="产品规格" prop="spec">
          <el-input v-model="form.spec" disabled placeholder="请输入产品规格" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" disabled placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="上传视频" prop="video">
          <!-- <el-input v-model="form.video" placeholder="请输入上传视频" /> -->
          <video v-if="form.video" :src="form.video" style="width:100%;height:200px;" controls></video>
        </el-form-item>
        <el-form-item label="产品图片" prop="images">
          <!-- <el-input v-model="form.images" placeholder="请输入产品图片" /> -->
          <div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;">
            <el-image
              style="width: 120px; height: 120px;padding:10px;"
              v-for="item in form.imagesArr"
              :key="item"
              :src="item"
              :preview-src-list="form.imagesArr">
            </el-image>
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status" disabled>
            <el-radio label="10">未处理</el-radio>
            <el-radio label="20">已处理</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier,dealSupplier, exportSupplier } from "@/api/system/supplier";

export default {
  name: "Supplier",
  data() {
    return {
      videoOpenUrl: '',
      videoOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 供应商表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        phone: undefined,
        category: undefined,
        spec: undefined,
        price: undefined,
        video: undefined,
        images: undefined,
        imagesArr: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        category: [
          { required: true, message: "供货类别不能为空", trigger: "blur" }
        ],
        spec: [
          { required: true, message: "产品规格不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        video: [
          { required: true, message: "上传视频不能为空", trigger: "blur" }
        ],
        images: [
          { required: true, message: "产品图片不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态  10=未处理  20=已处理不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "申请时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  watch: {
    'form.images'(newval) {
      this.form = {
        ...this.form,
        imagesArr: newval.split(',') || []
      }
    }
  },
  methods: {
    handleVideoClose() {
      this.$refs.popVideo.pause()
    },
    handleVideo(scope) {
      this.videoOpenUrl = scope.row.video
      this.videoOpen = true
    },
    /** 查询供应商列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then(response => {
        this.supplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        phone: undefined,
        category: undefined,
        spec: undefined,
        price: undefined,
        video: undefined,
        images: undefined,
        imagesArr: undefined,
        status: "0",
        createTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSupplier(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "供应商详情";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.open = false;
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != undefined) {
      //       updateSupplier(this.form).then(response => {
      //          this.msgSuccess("修改成功");
      //          this.open = false;
      //          this.getList();
      //       });
      //     } else {
      //       addSupplier(this.form).then(response => {
      //          this.msgSuccess("新增成功");
      //          this.open = false;
      //          this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除供应商编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSupplier(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },

    //商户更改商品状态
    handleDeal(row) {
      const ids = row.id ;
      this.$confirm('是否确认处理供应商编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return dealSupplier(row);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("处理成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有供应商数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSupplier(queryParams);
        }).then(response => {
          this.download(response.data);
        }).catch(function() {});
    }
  }
};
</script>
